package com.group01.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.SysDictData;
import com.group01.community.entity.SysDictType;
import com.group01.community.service.SysDictDataService;
import com.group01.community.utils.ExcelUtils;
import com.group01.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author iuoly
 * @since 2022-07-04
 */
@Api
@RestController
@RequestMapping("/sysDictData")
public class SysDictDataController {
    @Autowired
    private SysDictDataService sysDictDataService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseResult<String> add(@RequestBody SysDictData sysDictData) {
        sysDictData.setCreateTime(LocalDateTime.now());
        sysDictDataService.save(sysDictData);
        return ResponseResult.ok("添加成功");
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseResult<String> update(@RequestBody SysDictData sysDictData) {
        sysDictDataService.updateById(sysDictData);
        return ResponseResult.ok("修改成功");
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/{dictCodes}")
    public ResponseResult<String> delete(@PathVariable Long[] dictCodes) {
        sysDictDataService.removeByIds(Arrays.asList(dictCodes));
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("根据字典类型id查询字典数据")
    @GetMapping("/{dictId}")
    public ResponseResult<List<SysDictData>> selectDictDataByTypeId(@PathVariable Long dictId) {
        return ResponseResult.ok(sysDictDataService.selectDictDataByDictId(dictId));
    }

    @ApiOperation("查询指定字典类型")
    @GetMapping("/assignType")
    public ResponseResult<List<SysDictData>> selectAssignType() {
        return ResponseResult.ok(sysDictDataService.list(new QueryWrapper<SysDictData>().eq("dict_type", "sys_oper_type")));
    }

    @ApiOperation("查询所有字典类型")
    @GetMapping("/dictType")
    public ResponseResult<List<SysDictType>> selectDictType() {
        return ResponseResult.ok(sysDictDataService.selectDictType());
    }

    @ApiOperation("带条件查询")
    @PostMapping("/condition")
    public ResponseResult<IPage<SysDictData>> selectCondition(@RequestBody SysDictData sysDictData, Long current, Long size) {
        return ResponseResult.ok(sysDictDataService.selectCondition(sysDictData, current, size));
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseResult<List<SysDictData>> selectPage(Long current, Long size) {
        return ResponseResult.ok(sysDictDataService.selectPage(current, size));
    }

    @ApiOperation("根据id查询")
    @GetMapping("/id/{id}")
    public ResponseResult<SysDictData> selectDictDataById(@PathVariable Long id) {
        SysDictData dictData = sysDictDataService.getById(id);
        return ResponseResult.ok(dictData);
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseResult<String> export(HttpServletResponse resp) {
        ExcelUtils.export(SysDictData.class, sysDictDataService, resp, "字典数据信息");
        return ResponseResult.ok("ok");
    }
}

